package com.htfyun.hardware;

import android.util.Log;

import com.htfyun.hardware.reflect.Utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public enum WhiteListEnum {
    install("install"),
    uninstall("uninstall"),
    net("net"),
    ;
    private final String name;

    private WhiteListEnum(String name) {
        this.name = name;
    }

    private String getName() {
        return name;
    }

    private final Object mLock = new Object();
    private final int MAX_PAGE_SIZE = 800;//key-stringVal, stringVal max is 1023

    public void setEnabled(boolean enabled) {

        final String propertyKey = "persist." + getName() + "_wl.enabled";
        Utils.setSystemProperty(propertyKey, enabled ? "true" : "false");

        if (this == net) {
            HTFrameworksHelperController.getInstance().updateWhiteListForNet();
        }
    }

    public boolean isEnabled() {
//        if (this == net) {
//            return HTFrameworksHelperController.getInstance().NMS_isFirewallEnabled();
//        }
        final String propertyKey = "persist." + getName() + "_wl.enabled";
        String enabled = Utils.getSystemProperty(propertyKey);
        return enabled != null &&
                (enabled.equalsIgnoreCase("true")
                        || enabled.equalsIgnoreCase("1")
                        || enabled.equalsIgnoreCase("yes")
                        || enabled.equalsIgnoreCase("y")
                        || enabled.equalsIgnoreCase("on"));
    }

    public boolean isPrint() {
        final String propertyKey = "persist." + getName() + "_wl.print";
        String enabled = Utils.getSystemProperty(propertyKey);
        return enabled != null &&
                (enabled.equalsIgnoreCase("true")
                        || enabled.equalsIgnoreCase("1")
                        || enabled.equalsIgnoreCase("yes")
                        || enabled.equalsIgnoreCase("y")
                        || enabled.equalsIgnoreCase("on"));
    }

    public List<String> getWhiteListOfPackageName() {
        List<String> wl = new ArrayList<>();
        NVRomController nvrom = NVRomController.getInstance();
        String pageCountKey = "int_key_" + getName() + "_wl_page_count";
        String packageNamesPrefixKey = "str_key_" + getName() + "_wl_page_";
        synchronized (mLock) {
            int pageCount = nvrom.getNVRomInt(pageCountKey, -1);
            for (int i = 0; i < pageCount; i++) {
                String packageNames = nvrom.getNVRomString(packageNamesPrefixKey + i);
                if (packageNames == null || packageNames.length() == 0)
                    continue;

                wl.addAll(Arrays.asList(packageNames.replace(" ", "").split(";")));
            }
        }

        if (isPrint()) {
            Log.d("WhiteList", getName() + " white list is:" + wl);
        }

        return wl;
    }

    public void addPackageNamesIntoWhiteList(List<String> packageNames) {
        if (packageNames == null || packageNames.isEmpty()) {
            return;
        }
        boolean added = false;
        List<String> wl = getWhiteListOfPackageName();
        for (String pn : packageNames) {
            if (!wl.contains(pn)) {
                wl.add(pn);
                added = true;
            }
        }

        if (!added) {
            return;
        }

        putWhiteListIntoNVRom(wl);

        if (this == net) {
            HTFrameworksHelperController.getInstance().updateWhiteListForNet();
        }
    }

    public void removePackageNamesFromWhiteList(List<String> packageNames) {
        if (packageNames == null || packageNames.isEmpty()) {
            return;
        }
        int removedCount = 0;
        List<String> wl = getWhiteListOfPackageName();
        for (String pn : packageNames) {
            if (wl.remove(pn)) {
                removedCount++;
            }
        }

        if (removedCount == 0) {
            return;
        }

        putWhiteListIntoNVRom(wl);

        if (this == net) {
            HTFrameworksHelperController.getInstance().updateWhiteListForNet();
        }
    }

    private void putWhiteListIntoNVRom(List<String> wl) {
        String pageCountKey = "int_key_" + getName() + "_wl_page_count";
        String packageNamesPrefixKey = "str_key_" + getName() + "_wl_page_";
        synchronized (mLock) {
            NVRomController nvrom = NVRomController.getInstance();

            StringBuilder sb = new StringBuilder("");
            int count = 0;
            for (String pn : wl) {

                if (sb.length() + pn.length() + 1 >= MAX_PAGE_SIZE) {
                    if (sb.length() > 0) {
                        nvrom.setNVRomString(packageNamesPrefixKey + count, sb.toString());
                        count++;
                    }
                    sb = new StringBuilder("");
                }
                sb.append(pn);
                sb.append(";");
            }
            if (sb.length() > 0) {
                nvrom.setNVRomString(packageNamesPrefixKey + count, sb.toString());
                count++;
            }

            int previousCount = nvrom.getNVRomInt(pageCountKey, -1);
            if (count != previousCount) {
                nvrom.setNVRomInt(pageCountKey, count);
                if (previousCount > count) {
                    //remove other key
                    for (int i = count; i < previousCount; i++) {
                        nvrom.removeStringFromNVRomData(packageNamesPrefixKey + i);
                    }
                }
            }
        }
    }
}
